package com.walnut.lom_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.walnut.lom_system.common.enums.OrderEnum;
import com.walnut.lom_system.common.exception.InputException;
import com.walnut.lom_system.common.lang.Const;
import com.walnut.lom_system.common.util.CopyUtil;
import com.walnut.lom_system.domain.entity.EmployInfo;
import com.walnut.lom_system.domain.form.EmployInfoForm;
import com.walnut.lom_system.domain.form.EmployInfoSearchForm;
import com.walnut.lom_system.domain.vo.EmployInfoVo;
import com.walnut.lom_system.service.EmployInfoService;
import com.walnut.lom_system.mapper.EmployInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.exceptions.TooManyResultsException;
import org.mybatis.spring.MyBatisSystemException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author chenhetao
 * @description 针对表【employ_info】的数据库操作Service实现
 * @createDate 2022-04-17 00:07:14
 */
@Service
@Slf4j
public class EmployInfoServiceImpl extends ServiceImpl<EmployInfoMapper, EmployInfo>
        implements EmployInfoService {


    @Autowired
    private EmployInfoMapper employInfoMapper;

    @Override
    public int addEmployee(EmployInfoForm employInfoForm) {

        EmployInfo employInfo = CopyUtil.copy(employInfoForm, EmployInfo.class);


        UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (!Long.valueOf(principal.getUsername()).equals(employInfoForm.getSid())) {
            throw new InputException("学号与登录账号不同");
        }
        System.out.println(principal.getUsername());
//        principal.getUsername().equals(employInfoForm.ge)


        int insert = employInfoMapper.insert(employInfo);

        if (insert != 1) {
            return Const.OP_FAILED_INNER_ERR;
        } else {
            return Const.OP_SUCCESS;
        }
    }

    @Override
    public Page<EmployInfoVo> getList(EmployInfoSearchForm employInfoSearchForm) {

        EmployInfo employInfo = CopyUtil.copy(employInfoSearchForm, EmployInfo.class);
//        log.info(String.valueOf(employInfo));
//        QueryWrapper<EmployInfo> wrapper = new QueryWrapper<>();
//        type:1 name:2 gender:3 classNo:4 phone:5 email:6 scholarship:7 campaignIntent1:8 campaignIntent2:9 adjust:10
//
//        //愚蠢的条件构造
//        if (employInfo.getType() != null) {
//            wrapper.eq(OrderEnum.TYPE.getOrderName(), employInfo.getType());
//        }
//        if (employInfo.getName() != null) {
//            wrapper.like(OrderEnum.NAME.getOrderName(), employInfo.getName());
//        }
//        if (employInfo.getGender() != null) {
//            wrapper.eq(OrderEnum.GENDER.getOrderName(), employInfo.getGender());
//        }
//        if (employInfo.getClassNo() != null) {
//            wrapper.eq(OrderEnum.CLASS_NO.getOrderName(), employInfo.getClassNo());
//        }
//        if (employInfo.getPhone() != null) {
//            wrapper.like("phone", employInfo.getPhone());
//        }
//        if (employInfo.getEmail() != null) {
//            wrapper.like("email", employInfo.getEmail());
//        }
//        if (employInfo.getScholarship() != null) {
//            wrapper.eq("school", employInfo.getScholarship());
//        }
//        if (employInfo.getCampaignIntent1() != null) {
//            wrapper.like(OrderEnum.C_I_1.getOrderName(), employInfo.getCampaignIntent1());
//        }
//        if (employInfo.getCampaignIntent2() != null) {
//            wrapper.like(OrderEnum.C_I_2.getOrderName(), employInfo.getCampaignIntent2());
//        }
//        if (employInfo.getAdjust() != null) {
//            wrapper.eq(OrderEnum.ADJUST.getOrderName(), employInfo.getAdjust());
//        }
//        //判断排序方式
//        try {
//            if (employInfoSearchForm.getAsc() != null) {
//                if () {
//                    wrapper.orderByAsc();
//                } else {
//                    wrapper.orderByDesc(OrderEnum.valueOfName(employInfoSearchForm.getOrderBy()).getOrderName());
//                }
//            }
//        } catch (Exception e) {
//            log.error("排序方式不存在");
//            e.printStackTrace();
//        }
//        Page<EmployInfo> result = employInfoMapper.selectPage(new Page<>(employInfoSearchForm.getPageNum(), employInfoSearchForm.getPageSize()), wrapper);
        String orderName = null;
        if (employInfoSearchForm.getOrderBy() != null) {
            try {
                orderName = OrderEnum.valueOfName(employInfoSearchForm.getOrderBy()).getOrderName();
            } catch (NullPointerException e) {
                log.error("输入排序有误，采用默认排序");
            }
        }
        log.info(String.valueOf(employInfoSearchForm.getAsc()));
        Page<EmployInfo> result = employInfoMapper.queryConditionList(
                new Page<>(employInfoSearchForm.getPageNum(), employInfoSearchForm.getPageSize()),
                employInfo,
                orderName,
                employInfoSearchForm.getAsc()
        );

        return CopyUtil.dto2Vo(result, EmployInfoVo.class);
    }

    @Override
    public EmployInfoVo getEmployee(long id) {
        EmployInfo employInfo = employInfoMapper.selectById(id);
        EmployInfoVo employInfoVo = CopyUtil.copy(employInfo, EmployInfoVo.class);
        return employInfoVo;
    }

    @Override
    public int deleteEmployee(long id) {
        int i = employInfoMapper.deleteById(id);
        if (i != 0) {
            return Const.OP_SUCCESS;
        } else {
            return Const.OP_FAILED_INNER_ERR;
        }
    }

    @Override
    public int updateEmployee(long id, EmployInfoForm employInfoForm) {
        EmployInfo employInfo = CopyUtil.copy(employInfoForm, EmployInfo.class);
        employInfo.setId(id);
        int i = employInfoMapper.updateById(employInfo);
        if (i == 1) {
            return Const.OP_SUCCESS;
        } else {
            return Const.OP_FAILED_INNER_ERR;
        }
    }

    @Override
    public EmployInfoVo getOwnEmployeeByType(int type) throws Exception {
        UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println(principal.getUsername());
        EmployInfo employInfo = new EmployInfo();
        employInfo.setSid(Long.valueOf(principal.getUsername()));
        employInfo.setType(type);
        EmployInfo record;
        try {
            record = employInfoMapper.selectByRecord(employInfo);
        } catch (MyBatisSystemException e) {
            log.error("查询到多于1条的记录:期望1条");
            throw new Exception("查询到多于1条的记录:期望1条");
        }

        return CopyUtil.copy(record, EmployInfoVo.class);
    }

    @Override
    public int deleteOwnEmployeeByType(int type) {
        QueryWrapper<EmployInfo> wrapper = new QueryWrapper<>();

        wrapper
                .eq("type", type)
                .eq("sid", Long.valueOf(((UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername()));
        int delete = employInfoMapper.delete(wrapper);
        if (delete == 1) {
            return Const.OP_SUCCESS;
        } else {
            return Const.OP_NO_EFFECT;
        }
    }

    @Override
    public int updateOwnEmployeeByType(int type, EmployInfoForm employInfoForm) {
        if (employInfoForm.getType() != null) {
            //试图修改类型，拒绝操作
            return Const.OP_DENIED;
        }

        EmployInfo copy = CopyUtil.copy(employInfoForm, EmployInfo.class);

        UpdateWrapper<EmployInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper
                .eq("type", type)
                .eq("sid", Long.valueOf(((UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername()));

        int i = employInfoMapper.update(copy, updateWrapper);

        if (i == 1) {
            return Const.OP_SUCCESS;
        } else {
            return Const.OP_NO_EFFECT;
        }
    }
}




